package com.jn.algorithm.day004.question11;

/**
 * @author 江南大学1033190417
 * @date 2022/4/18 14:53
 */
public class Solution {

    //一般想到的方法暴力解法，但是非常耗时
    public int maxArea(int[] height) {
        int length = height.length;
        int area = 0;
        for (int i = 0; i < length - 1; i++) {
            for (int j = i + 1; j < length; j++) {
                int weight = j - i;
                int high = Math.min(height[i], height[j]);
                area = Math.max(area, weight * high);
            }
        }
        return area;
    }

    //双指针
    /*
       左右两个指针
       每次移动纵坐标对应小的那个指针
     */
    public int maxArea2(int[] height) {
        int area = 0;
        int l = 0, r = height.length - 1;
        while (l < r) {
            area = Math.max(area, Math.min(height[l], height[r]) * (r - l));
            if (height[l] >= height[r])
                r--;
            else
                l++;
        }
        return area;
    }
}
